﻿using DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using word.winform;
using Models;
namespace word.UserControls
{
    public partial class ShowData : UserControl
    {
        /// <summary>
        /// DataGridView的列键值对
        /// </summary>
        Dictionary<String,String> table;
        /// <summary>
        /// 查询表名
        /// </summary>
        public String tableName;
       
        public ShowData(Dictionary<String, String> table,String tableName)
        {
            InitializeComponent();
            this.table = table;
            this.tableName = tableName;
        }

        public ShowData() { }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ManagerGoodsType_Load(object sender, EventArgs e)
        {
            init();
            load();
        }

        public void init()
        {
            
            foreach (KeyValuePair<string, string> kvp in table)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = kvp.Key;
                column.DataPropertyName = kvp.Value.ToString();
                dataGridView.Columns.Add(column);
            }

            
        }
        public void load()
        {
            String sql = "SELECT * FROM " + tableName;
            foreach (KeyValuePair<String,String> item in table)
            {
                TSCB_name.Items.Add(item.Key);
            }
            
            dataGridView.DataSource = DBHelper.Select(sql);
        }

        public void load(String sql)
        {
            try
            {
                dataGridView.DataSource = DBHelper.Select(sql);

            }
            catch (Exception ex)
            {
                MessageBox.Show("查询语句有误" + ex.Message, "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return;
            }
        }



        /// <summary>
        ///  实现DataGridView修改同步到数据库中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(DBHelper.GetSqlDataAdapter());
            try
            {
                DBHelper.GetSqlDataAdapter().Update(DBHelper.GetDataSet());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
        }

        private void TSB_search_Click(object sender, EventArgs e)
        {
            String columnName = null;
            foreach (KeyValuePair<String,String> item in table)
            {
                if (item.Key == TSCB_name.SelectedItem.ToString())
                {
                    columnName = item.Value;
                }
            }
            String sql = $"SELECT * FROM {tableName} where {columnName} like '{TSTB_value}%'";
            load(sql);
        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            SqlSelect sqlSelect = new SqlSelect();
            sqlSelect.setFormTextValue += new setTextValue(load);
            sqlSelect.TableName = tableName;
            sqlSelect.Show();
            
        }
    }
}
